package fr.mairie.parc.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import fr.mairie.parc.dao.UserDao;
import fr.mairie.parc.dao.mapper.UserRowMapper;
import fr.mairie.parc.domain.User;

public class UserDaoImpl implements UserDao{


	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public JdbcTemplate getJdbcTempalete() {
		return jdbcTemplate;
	}

	@Autowired
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<User> selectAll() {
		return jdbcTemplate.query("SELECT idUser, idMateriel, nom, prenom, direction, service, batiment, etage, bureau" +
				"" + " FROM user",
						new UserRowMapper());
	}

	@Override
	public User getUserById(int id) {
		return (User) jdbcTemplate.queryForObject("SELECT * FROM user WHERE idUser=?",
				new Object [] {id},
				new UserRowMapper());
	}

	@Override
	public void addUser(User user) {
		jdbcTemplate.update("INSERT INTO user (idMateriel, nom, prenom, direction, service, batiment, etage, bureau)" +
				"VALUES (?,?,?,?,?,?,?,?)",
    new Object [] {
				   user.getIdMateriel(),
				   user.getNom(),
				   user.getPrenom(),
				   user.getDirection(),
				   user.getService(),
				   user.getBatiment(),
				   user.getEtage(),
				   user.getBureau()

                   });

	}

	@Override
	public void updateUser(User user) {
		jdbcTemplate.update("UPDATE user SET idMateriel = ? AND direction=? AND " +
				" service=? AND batiment=? AND etage=? AND aile=? AND bureau=? WHERE idUser = ? ",
                new Object [] {user.getIdMateriel(),
							   user.getIdUser(),
							   user.getDirection(),
							   user.getService(),
							   user.getBatiment(),
							   user.getEtage(),
							   user.getBureau()
				});

	}

	@Override
	public void deleteUser(User user) {
		jdbcTemplate.update("DELETE FROM user WHERE idUser= ?",
				new Object[] {user.getIdUser()});

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> selectDirection() {
		return jdbcTemplate.query("SELECT direction" +
				"" + " FROM emplacement",
						new UserRowMapper());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> selectService() {
		return jdbcTemplate.query("SELECT service" +
				"" + " FROM emplacement",
						new UserRowMapper());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> selectBatiment() {
		return jdbcTemplate.query("SELECT batiment" +
				"" + " FROM emplacement",
						new UserRowMapper());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> getUserbyName(String name) {
		return jdbcTemplate.query("SELECT * FROM user WHERE nom=?",
				new Object [] {name},
						new UserRowMapper());
	}

	@Override
	public User getUserById(String id) {
		return (User) jdbcTemplate.queryForObject("SELECT * FROM user WHERE idUser=?",
				new Object [] {id},
				new UserRowMapper());
	}

}
